﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using ARR.UI.Models;
using ARR.Shared;
using ARR.UI.Commands;
using System.Collections.ObjectModel;
using ARR.Entities;
using Telerik.Windows.Controls;

namespace ARR.UI.ViewModels
{
    public class PlanogramsMasterViewModel : WorkspaceViewModel
    {
        private IPlanogramsMasterModel model;
        private ICommand selectionChangedCommand;
        private ICommand saveCommand;
        private ICommand cancelCommand;
        private ICommand deleteCommand;



        public PlanogramsMasterViewModel(IPlanogramsMasterModel model)
        {
            this.DisplayName = Labels.MaestroDePlanogramas;
            this.model = model;
            
            SelectedPlanograms = new List<Planogram>();
        }



        public ICommand SelectionChangedCommand
        {
            get
            {
                if (selectionChangedCommand == null)
                {
                    selectionChangedCommand = new RelayCommand(e => GetSelectedItems(e as SelectionChangeEventArgs));
                }
                return selectionChangedCommand;
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(e => Save());
                }
                return saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new RelayCommand(e => Cancel());
                }
                return cancelCommand;
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new RelayCommand(e => Delete());
                }
                return deleteCommand;
            }
        }

        public ObservableCollection<Planogram> Planograms
        {
            get
            {
                return new ObservableCollection<Planogram>(model.Planograms);
            }
        }

        public ObservableCollection<Store> Stores
        {
            get
            {
                return model.Stores;
            }
        }

        public ObservableCollection<PlanogramLocation> PlanogramLocations
        {
            get
            {
                return new ObservableCollection<PlanogramLocation>(model.PlanogramLocations);
            }
        }
    

        public Array PlanogramTypes
        {
            get
            {
                return Enum.GetValues(typeof(PlanogramType));
            }
        }

        public List<Planogram> SelectedPlanograms { get; set; }


        public void TriggerPropertyChanged()
        {
            var aux = model.Planograms;
            model.Planograms = null;
            OnPropertyChanged<PlanogramsMasterViewModel>(p => p.Planograms);

            model.Planograms = aux;
            OnPropertyChanged<PlanogramsMasterViewModel>(p => p.Planograms);
        }

        private void GetSelectedItems(SelectionChangeEventArgs e)
        {
            try
            {
                List<Planogram> added = e.AddedItems.Cast<Planogram>().ToList();
                List<Planogram> removed = e.RemovedItems.Cast<Planogram>().ToList();

                SelectedPlanograms.AddRange(added);
                removed.ForEach(at => SelectedPlanograms.Remove(at));
            }
            catch
            {
            }
        }

        private void Save()
        {
            if (AreValid())
            {
                model.Save();
                model.LoadPlanograms();

                OnPropertyChanged<PlanogramsMasterViewModel>(p => p.Planograms);
            }
        }

        private void Cancel()
        {
            model.LoadPlanograms();

            OnPropertyChanged<PlanogramsMasterViewModel>(p => p.Planograms);
        }

        private void Delete()
        {

        }

        private bool AreValid()
        {
            foreach (Planogram p in Planograms)
            {
                if (!p.IsValid)
                    return false;
            }

            return true;
        }
    }
}
